package com.oa.controller;

import com.oa.back.ResponseBack;
import com.oa.common.annotion.Interceptor;
import com.oa.common.enums.CodeStatus;
import com.oa.common.enums.apps.FromFileType;
import com.oa.service.AppsTypeService;
import com.oa.service.TplHtmlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/manage/tplhtml")
public class TplHtmlController extends BaseController {

    private static final Logger logger= LoggerFactory.getLogger(TplHtmlController.class);

    @Autowired
    private AppsTypeService typeService;

    @Autowired
    private TplHtmlService tplHtmlService;

    @RequestMapping(value = "index.html",method = RequestMethod.GET)
    public String index(ModelMap modelMap,Long typeId,Long type){
        modelMap.addAttribute("appsType",typeService.get(typeId));
        modelMap.addAttribute("fileTypes",FromFileType.values());
        modelMap.addAttribute("type",type);
        modelMap.addAttribute("template",tplHtmlService.getTpl(typeId));
        modelMap.addAttribute("typeId",typeId);
        return "/admin/tplhtml/index";
    }

    @RequestMapping(value = "{method}.html",method = RequestMethod.GET)
    public String method(ModelMap modelMap, @PathVariable(name = "method")String method,Long typeId,Long tpltype){
        modelMap.addAttribute("typeId",typeId);
        modelMap.addAttribute("type",tpltype);
        return "/admin/tplhtml/"+method;
    }

    @RequestMapping(value = "submit",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "提交模板的函数")
    public ResponseBack<String> submit(String params){
        try{
            return tplHtmlService.submit(params);
        }catch (RuntimeException ex){
            logger.error("提交审核模板接口的异常:",ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }
}
